<template>
  <div>
    <div>
      <el-button type="primary" @click="submit()">提交</el-button>
    </div>
    <div>
      <el-card>
        <el-form :model="work" label-width="80px">
          <el-form-item label="任务名称">
            <el-input
              v-model="work.item"
              placeholder="请输入任务名称"
              type="textarea"
              rows="1"
              maxlength="64"
            ></el-input>
          </el-form-item>
          <el-form-item label="任务简述">
            <el-input
              v-model="work.content"
              placeholder="请输入任务的简要概述"
              type="textarea"
              maxlength="300"
            ></el-input>
          </el-form-item>
          <el-form-item label="项目阶段">
            <el-select v-model="work.stage" placeholder="请选择项目阶段">
              <el-option
                v-for="stage in stages"
                :key="stage.sequence"
                :label="stage.item"
                :value="stage.item"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-card>
      <el-card>
        <div>
          <el-button type="primary" icon="el-icon-plus" @click="addFormVisible=true">创建工作内容</el-button>
        </div>
        <div>
          <h3>任务内容</h3>
          <li v-for="detail in work.details" :key="detail">{{detail}}</li>
        </div>
        <div>
          <h3>任务要求</h3>
          <li v-for="target in work.targets" :key="target">{{target}}</li>
        </div>
      </el-card>
    </div>
    <el-dialog title="新增任务内容" :visible.sync="addFormVisible">
      <el-form :model="addContent">
        <el-form-item label="任务内容">
          <el-input
            v-model="addContent.content"
            placeholder="请填写任务内容"
            type="textarea"
            maxlength="100"
          ></el-input>
        </el-form-item>
        <el-form-item label="类型">
          <el-radio-group v-model="addContent.leixing">
            <el-radio-button label="项目内容"></el-radio-button>
            <el-radio-button label="项目要求"></el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-button type="primary" @click="addDetail()">增加</el-button>
        <el-button type="primary" @click="addFormVisible = false">取消</el-button>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getprojectinfos, postprojectinfos } from "@/api/table";

export default {
  data() {
    return {
      project: null,
      addFormVisible: false,
      work: {
        item: null,
        content: null,
        stage: null,
        details: [],
        targets: []
      },
      addContent: {
        content: null,
        leixing: null
      },
      stages: []
    };
  },
  created() {
    this.project = this.$route.params.pjname;
    this.getstages();
  },
  methods: {
    addDetail() {
      if (this.addContent.leixing == "项目内容") {
        this.work.details.push(this.addContent.content);
      } else {
        this.work.targets.push(this.addContent.content);
      }
    },
    submit() {
      postprojectinfos(this.project, "create_work", this.work).then(
        response => {
          if (response.data.success === 1) {
            this.$message({
              message: "提交成功",
              type: "success"
            });
          }
        }
      );
    },
    getstages() {
      getprojectinfos(this.project, "stage").then(response => {
        this.stages = response.data.stages;
      });
    }
  }
};
</script>

